package com.wannengbang.cloudleader.mine;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.RecyclerView;

import com.ethanhua.skeleton.Skeleton;
import com.ethanhua.skeleton.SkeletonScreen;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.base.BaseActivity;
import com.wannengbang.cloudleader.base.DataCallBack;
import com.wannengbang.cloudleader.bean.ProductClassifyBean;
import com.wannengbang.cloudleader.homepage.ProductListActivity;
import com.wannengbang.cloudleader.homepage.adapter.ProductClassifyListAdapter;
import com.wannengbang.cloudleader.mine.model.IMineModel;
import com.wannengbang.cloudleader.mine.model.MineModelImpl;
import com.wannengbang.cloudleader.widget.AppTitleBar;
import com.wannengbang.cloudleader.widget.WrapContentLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ProductClassifyActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private IMineModel mineModel;
    private ProductClassifyListAdapter listAdapter;
    private List<ProductClassifyBean.DataBean> beanList;
    private SkeletonScreen skeletonScreen;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_classify);
        ButterKnife.bind(this);
        initView();
    }


    public void initView() {
        mineModel = new MineModelImpl();
        beanList = new ArrayList<>();
        listAdapter = new ProductClassifyListAdapter(beanList);
        recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(mActivity));
        recyclerView.setAdapter(listAdapter);
        listAdapter.setOnCallBackListener((type, position) -> {
            Intent intent = new Intent(mActivity, ProductListActivity.class);
            intent.putExtra("type", beanList.get(position).getType());
            intent.putExtra("typeName", beanList.get(position).getName());
            intent.putExtra("pic_info", beanList.get(position).getPic_info());
            startActivity(intent);
        });
        skeletonScreen = Skeleton.bind(recyclerView)
                .adapter(listAdapter)
                .color(R.color.shimmer_color_1)
                .shimmer(true)
                .angle(20)
                .frozen(false)
                .duration(1000)
                .count(20)
                .load(R.layout.item_skeleton_classify_list)
                .show();
        requestData();
    }

    public void requestData() {
        mineModel.requestShopClass(new DataCallBack<ProductClassifyBean>() {
            @Override
            public void onSuccessful(ProductClassifyBean bean) {
                if (bean.getData() != null && bean.getData().size() > 0) {
                    beanList.addAll(bean.getData());
                }
                if (beanList.size() > 0) {
                    llNoData.setVisibility(View.GONE);
                } else {
                    llNoData.setVisibility(View.VISIBLE);
                }
                listAdapter.notifyDataSetChanged();
                skeletonScreen.hide();
            }

            @Override
            public void onFailed(String msg, String code) {
                skeletonScreen.hide();
            }
        });
    }
}
